<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class StaffInfo
 * @package App\Models
 * @version December 7, 2018, 7:05 pm CST
 *
 * @property integer oid
 * @property string name
 * @property string avatar
 * @property string idno
 * @property string mobile
 * @property string vpl_number
 * @property string id_card
 * @property string driving_license
 * @property string vehicle_license
 * @property string vehicle_license_b
 * @property boolean vehicle_type
 * @property string apron_card
 * @property string attachment
 * @property string comment
 * @property boolean autherized
 * @property boolean status
 */
class StaffInfo extends Model
{
    use SoftDeletes;

    public $table = 'staff_info';
    
    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';


    protected $dates = ['deleted_at'];

    protected $appends = ['orgs', 'card_fee', 'ocr_info']; 


    public $fillable = [
        'oid',
        'name',
        'avatar',
        'idno',
        'mobile',
        'vpl_number',
        'id_card',
        'driving_license',
        'vehicle_license',
        'vehicle_license_b',
        'vehicle_type',
        'apron_card',
        'attachment',
        'comment',
        'autherized',
        'status'
    ];


    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
        'oid' => 'integer',
        'name' => 'string',
        'avatar' => 'string',
        'idno' => 'string',
        'mobile' => 'string',
        'vpl_number' => 'string',
        'id_card' => 'string',
        'driving_license' => 'string',
        'vehicle_license' => 'string',
        'vehicle_license_b' => 'string',
        'vehicle_type' => 'integer',
        'apron_card' => 'string',
        'attachment' => 'array',
        'comment' => 'string',
        'autherized' => 'integer',
        'status' => 'integer'
    ];


    // 需要上传的字段
    public static $fieldAllowedUpload = [
        'avatar', 
        'id_card', 
        'driving_license', 
        'vehicle_license', 
        'vehicle_license_b',
        'apron_card',
        'attachment',
    ];

    // 必须上传的字段
    public static $fieldMustUpload = [
        'id_card', 
        'driving_license', 
        'vehicle_license', 
        'vehicle_license_b',
    ];

    // 需要清空的字段
    public static $fieldNeedClear = [
        'id_card', 
        'driving_license', 
        'vehicle_license', 
        'vehicle_license_b',
        'apron_card',
        'attachment',
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'oid' => 'required',
        'name' => 'required',
        'idno' => 'bail|required|unique:staff_info',
        'mobile' => 'bail|required|unique:staff_info',
        'vpl_number' => 'bail|required|unique:staff_info',
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rulesUpdate = [
    ];


    /**
     * 获取其它附件
     *
     * @param  string $value
     * @return void
     */
    public function getAttachmentAttribute() {
        return empty($this->attributes['attachment']) 
                    ? []
                    : json_decode($this->attributes['attachment'], true);
    }


    /**
     * 获取组织结构树
     *
     * @param  string $value
     * @return void
     */
    public function getOrgsAttribute() {
        return $this->attributes['orgs'] = StaffOrg::getNodes($this->attributes['oid']);
    }

    /**
     * 获取组织结构树
     *
     * @param  string $value
     * @return void
     */
    public function getCardFeeAttribute() {
        $field = [
            'fee_annual', 
            'fee_annual_oversize', 
            'fee_monthly', 
            'fee_monthly_oversize'
        ];
        return $this->attributes['card_fee'] = StaffOrg::select($field)->where('id', $this->attributes['oid'])->first();
    }

    /**
     * 获取组织结构树
     *
     * @param  string $value
     * @return void
     */
    public function getOcrInfoAttribute() {
        $field = [
                'id_card_no',
                'id_card_name',
                'driving_license_idno',
                'driving_license_name',
                'vehicle_license_vpl',
                'vehicle_license_name',
                'vehicle_license_b_vpl',
                'vehicle_capacity',
            ];
        $ocr = StaffInfoOcr::select($field)->where('uid', $this->attributes['id'])->first();
        if (!$ocr) {
            $ocr = array_fill_keys($field, '');
        } else {
            $ocr = $ocr->toArray();
        }
        return $this->attributes['ocr_info'] = $ocr;
    }
    
    // 核定载客数 七座以下，以上
    public static function vehicleCapacityToType($vehicle_capacity = '')
    {
        return intval($vehicle_capacity) > 7 ? 2 : 1;
    }
}
